<?php

namespace app\controllers;
use yii\web\Controller;
use Yii;
use app\models\comment;
use app\models\content;
use app\models\login;

class CommentController extends Controller{
	public $enableCsrfValidation = false;
	//信息发布
	public function actionGet(){
		$model = new comment();
		$content = new content();
	    if(Yii::$app->request->isPost){
		$postData = Yii::$app->request->bodyParams;
		$content_id = $postData['id'];
	         //获取内容
                $c_arr = $content->find()->select('*')->where('state = :state',[":state"=>'1'])->andwhere('content_id =:content_id',[":content_id"=>$content_id])->asArray()->all();
		if(!empty($c_arr)){
		 //获取评论
	        $p_arr = $model->find()->select('*')->where('state = :state',[":state"=>'0'])->andwhere('content_id = :content_id',[":content_id"=>$content_id])->orderby('comment_id desc')->asArray()->all();
		$p_num = $model->find()->select('count(*)')->where('state = :state', [":state"=>'0'])->andwhere('content_id = :content_id',[":content_id"=>$content_id])->asArray()->all();
		$p_num = (int)$p_num[0]['count(*)'];
				
		if($p_num == 0){
		   $data = array('content' => $c_arr, 'count_p' => $p_num);
		   echo json_encode(array('error' => 0,'message' => 'success', 'data' =>$data));
		}else{
		   $modcontent = array('comment' => $p_num);
		   $content->mod($content_id,$modcontent);
		   $data = array('content' => $c_arr, 'comment' => $p_arr, 'count_p' => $p_num);
		   echo json_encode(array('error' => 0, 'message' => 'success', 'data' => $data));
				}
			}else{
			    $data = array('content' => '没有这段内容');
			    echo json_encode(array('error' => 1, 'message' => 'fail','data' => $data));
			}
		}
                   exit(0);
	}
		//信息接收&&数据库输入
    public function actionPublish(){
          $model = new comment();
          $content = new content();
          if(Yii::$app->request->isPost ){
               //输入数据
            $postData = Yii::$app->request->post();
            $content_id = $postData['id'];
	    //$openid = $postData['username'];
	       //检查content_id是否存在
	    $checkcontent = $content->find()->select('*')->where('state = :state',[":state"=>'1'])->andwhere('content_id = :content_id',[":content_id"=>$content_id])->asArray()->all();
	    if(!empty($checkcontent)){
	       $result = $model->add($postData);
	       if( $result == 0 ){
		  $data = array('content' => '提交数据成功');
		  echo json_encode(array('error' => 0, 'message' => 'success', 'data' => $data));
		}else{
		  $data = array('Content' => '提交数据失败,请@留言,联系公众号~', 'content' => $result);
                  echo json_encode(array('error' => 1, 'message' =>'fail', 'data' => $data));
        	    }
	        } else{
		  $data = array('content' => '错误的contentid');
		  echo json_encode(array('error' => -1, 'message' => 'fail', 'data' => $data));
		   }
		 }
               exit(0);
	  }
	  
	 //获取特定用户名所有内容评论=>收信功能
         //需要评论者的sex ,nickname,comment,createtime, 特定用户的content 
    public function actionLetter(){
         $model = new comment(); 
         $content = new content();			
         if(Yii::$app->request->isPost ){
             $postData = Yii::$app->request->post();
	     $openid = $postData['username'];
  	     $sqla = "select * from wall_comment where content_id in(select content_id from wall_content where openid = '$openid') and state =0 order by 'comment_id' desc";    
	      $data = comment::findBysql($sqla)->asArray()->all();
              $a[]=array('comment'=>$data);
           
              $sqlb = "select content_id from wall_comment where content_id in(select content_id from wall_content where openid = '$openid') and state = 0 order by 'comment_id' desc";
              $dataa = comment::findBysql($sqlb)->asArray()->all();

              foreach($dataa as $key => $val){
                   foreach($val as $kkey => $vval){
                   $sql = "select content_id,content from wall_content where content_id = '$vval'";
                   $data_a = content::findBysql($sql)->asArray()->all();
                   $b[]=array('content'=>$data_a);
             }
        }                
              $sqlc = "select openid from wall_comment where content_id in(select content_id from wall_content where openid = '$openid') and state = 0 order by 'comment_id' desc";
              $datab = comment::findBysql($sqlc)->asArray()->all();

              foreach( $datab as $akey => $aval){
                  foreach($aval as $akkey => $avval){
                  $sql = "select openid,sex,nickname from wall_user where openid ='$avval'";
                  $data_b = login::findBysql($sql)->asArray()->all();
                  $c[]=array('user'=>$data_b);
                   } 
               }
                //统一成一个数组输出，json才不会乱
                //一个接口一个echo
                 $sum[] = array_merge($a,$b,$c);
               echo json_encode(array('error' => 0, 'message' => 'success','data_sum' => $sum,));
            }
           exit (0);
	}
   	
	 //修改评论state为0，表示用户已读，在收信栏不再显示
    public function actionUpstate(){
	      $model = new comment();
	      if(Yii::$app->request->isPost ){
		  $postData =Yii::$app->request->post();
                  $result = $model->del($postData);
                  if( $result == 0 ){
                       $data = array('content' => '已读评论');
                       echo json_encode(array('error' => 0,'message' => 'success','data' => $data));           
                 }else{
                       $data = array('Content'=>'操作失败','content'=>$result);
                       echo json_encode(array('error' => 1,'message' => 'fail','data'=>$data));
	   
                    }
	       }
                   exit(0);
         }             
    			 			
     }

